﻿#light
        
open System.IO
open System.Net
open Microsoft.FSharp.Control.CommonExtensions 
open System.Threading
open System.Threading.Tasks;
open System.Threading.Collections
open System

type mypair ={url: System.String; text : System.String}

let download (array : ResizeArray<string>) =
  seq { use results = new BlockingCollection<(string * string)>()
        use pagesRemain = new CountdownEvent(1)
        
        let urls = List.of_seq(array)
        
        let _ = Task.Create(fun _ ->
          urls |> List.iter(fun url ->
            let wc = new WebClient()
            wc.DownloadStringCompleted.Add(fun args ->
              if args.Error = null then
                results.Add(((args.UserState :?> string), args.Result))
              if pagesRemain.Decrement() then
                results.CompleteAdding()
            )
      
            pagesRemain.Increment()
            wc.DownloadStringAsync(new Uri(url), url)
          )
    
          if pagesRemain.Decrement() then results.CompleteAdding()
        )
  
        for result in results.GetConsumingEnumerable() do yield result
      }